namespace TXLooker.WorkItemExtention.WIQL
{
    using System;
    using System.Collections;

    public sealed class Parser
    {
        private Parser()
        {
        }

        public static ArrayList ParseLexems(string s)
        {
            ArrayList list = new ArrayList();
            int length = s.Length;
            int index = 0;
            while (index < length)
            {
                if (char.IsWhiteSpace(s, index) || char.IsControl(s, index))
                {
                    index++;
                    continue;
                }
                if (char.IsLetter(s, index) || (s[index] == '_'))
                {
                    int startIndex = index;
                    while ((index < length) && ((char.IsLetterOrDigit(s, index) || (s[index] == '_')) || (s[index] == '.')))
                    {
                        index++;
                    }
                    NodeName name = new NodeName(s.Substring(startIndex, index - startIndex));
                    name.StartOffset = startIndex;
                    name.EndOffset = index;
                    list.Add(name);
                    continue;
                }
                if (s[index] == '@')
                {
                    int num4 = ++index;
                    while ((index < length) && char.IsLetterOrDigit(s, index))
                    {
                        index++;
                    }
                    NodeVariable variable = new NodeVariable(s.Substring(num4, index - num4));
                    variable.StartOffset = num4 - 1;
                    variable.EndOffset = index;
                    list.Add(variable);
                    continue;
                }
                if (s[index] == '[')
                {
                    int num5 = ++index;
                    int num6 = length;
                    bool flag = true;
                    while (index < length)
                    {
                        if (s[index] == ']')
                        {
                            num6 = index++;
                            flag = false;
                            break;
                        }
                        index++;
                    }
                    NodeName node = new NodeName(s.Substring(num5, num6 - num5));
                    node.StartOffset = num5 - 1;
                    node.EndOffset = num6 + 1;
                    if (flag)
                    {
                        throw new SyntaxException(node, SyntaxError.ExpectingClosingSquareBracket);
                    }
                    list.Add(node);
                    continue;
                }
                if ((((s[index] == '-') || (s[index] == '+')) && (((index + 1) < length) && char.IsDigit(s, index + 1))) || char.IsDigit(s, index))
                {
                    int num7 = index;
                    if ((s[index] == '-') || (s[index] == '+'))
                    {
                        index++;
                    }
                    while ((index < length) && char.IsDigit(s[index]))
                    {
                        index++;
                    }
                    if ((index < length) && (s[index] == '.'))
                    {
                        index++;
                        while ((index < length) && char.IsDigit(s[index]))
                        {
                            index++;
                        }
                    }
                    if ((index < length) && ((s[index] == 'e') || (s[index] == 'E')))
                    {
                        index++;
                        if ((index < length) && ((s[index] == '-') || (s[index] == '+')))
                        {
                            index++;
                        }
                        while ((index < length) && char.IsDigit(s[index]))
                        {
                            index++;
                        }
                    }
                    NodeNumber number = new NodeNumber(s.Substring(num7, index - num7));
                    number.StartOffset = num7;
                    number.EndOffset = index;
                    list.Add(number);
                    continue;
                }
                if ((s[index] == '\'') || (s[index] == '"'))
                {
                    char ch = s[index++];
                    int num8 = index;
                    int num9 = length;
                    bool flag2 = true;
                    while (index < length)
                    {
                        if (s[index] == ch)
                        {
                            index++;
                            if ((index == length) || (s[index] != ch))
                            {
                                num9 = index - 1;
                                flag2 = false;
                                break;
                            }
                        }
                        index++;
                    }
                    string newValue = ch.ToString();
                    NodeString text2 = new NodeString(s.Substring(num8, num9 - num8).Replace(newValue + newValue, newValue));
                    text2.StartOffset = num8 - 1;
                    text2.EndOffset = num9 + 1;
                    if (flag2)
                    {
                        throw new SyntaxException(text2, SyntaxError.ExpectingClosingQuote);
                    }
                    list.Add(text2);
                    continue;
                }
                if (((index + 1) < length) && (((((s[index] == '<') && ((s[index + 1] == '=') || (s[index + 1] == '>'))) || ((s[index] == '>') && (s[index + 1] == '='))) || (((s[index] == '!') && (s[index + 1] == '=')) || ((s[index] == '=') && (((s[index + 1] == '=') || (s[index + 1] == '>')) || (s[index + 1] == '<'))))) || (((s[index] == '&') && (s[index + 1] == '&')) || ((s[index] == '|') && (s[index + 1] == '|')))))
                {
                    NodeOperation operation = new NodeOperation(s.Substring(index, 2));
                    operation.StartOffset = index;
                    operation.EndOffset = index + 2;
                    list.Add(operation);
                    index += 2;
                }
                else
                {
                    NodeOperation operation2 = new NodeOperation(s.Substring(index, 1));
                    operation2.StartOffset = index;
                    operation2.EndOffset = index + 1;
                    list.Add(operation2);
                    index++;
                }
            }
            return list;
        }

        public static NodeSelect ParseSyntax(string s)
        {
            Scanner scanner = new Scanner(ParseLexems(s));
            NodeSelect select = scanner.Scan();
            scanner.CheckTail();
            return select;
        }
    }
}

